package com.cgame.game;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cgame.vo.Citizen;
import com.cgame.vo.Forsee;
import com.cgame.vo.Guard;
import com.cgame.vo.Hunter;
import com.cgame.vo.Idiot;
import com.cgame.vo.Player;
import com.cgame.vo.Witch;
import com.cgame.vo.Wolf;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class Game {
	public String gamename;
	public int number;		//玩家人数
	public JsonArray rolelist;	//角色列表
//	public List<Player> playerList=new ArrayList<Player>();
	public Night night;
	public int day=1;
	//临时map
	public Map<String, Player> playerMap=new HashMap<String, Player>();
	private boolean isnight=false;
	public Player witch;
	public Player guard;
	public Player forsee;
	public Player hunter;
	public String godword;
	public String finish;
	private int wolfnumber;
	private int citizennumber;
	public Game creategame(JsonObject param){
		//初始化
		wolfnumber=param.get("wolfnumber").getAsInt();
		citizennumber=param.get("citizennumber").getAsInt();
		boolean forsee=param.get("forsee")==null?false:param.get("forsee").getAsBoolean();
		boolean witch=param.get("witch")==null?false:param.get("witch").getAsBoolean();
		boolean hunter=param.get("hunter")==null?false:param.get("hunter").getAsBoolean();
		boolean guard=param.get("guard")==null?false:param.get("guard").getAsBoolean();
		boolean idiot=param.get("idiot")==null?false:param.get("idiot").getAsBoolean();
		//计算number
		number=0;
		number+=wolfnumber;
		number+=citizennumber;
		number+=(forsee?1:0)+(witch?1:0)+(hunter?1:0)+(idiot?1:0)+(guard?1:0);
		//生成角色列表
		rolelist=new JsonArray();
		rolelist.add(new Gson().fromJson("{'key':'wolf','value':'狼人'}", JsonObject.class));
		rolelist.add(new Gson().fromJson("{'key':'citizen','value':'村民'}", JsonObject.class));
		if(forsee){
			rolelist.add(new Gson().fromJson("{'key':'forsee','value':'预言家'}", JsonObject.class));
		}
		if(witch){
			rolelist.add(new Gson().fromJson("{'key':'witch','value':'女巫'}", JsonObject.class));
		}
		if(hunter){
			rolelist.add(new Gson().fromJson("{'key':'hunter','value':'猎人'}", JsonObject.class));
		}
		if(guard){
			rolelist.add(new Gson().fromJson("{'key':'guard','value':'守卫'}", JsonObject.class));
		}
		if(idiot){
			rolelist.add(new Gson().fromJson("{'key':'idiot','value':'白痴'}", JsonObject.class));
		}
		//生成名字
		gamename=wolfnumber+"狼人/"+citizennumber+"平民/"+
				(forsee?"预言家/":"")+
				(witch?"女巫/":"")+
				(hunter?"猎人/":"")+
				(idiot?"白痴/":"")+
				(guard?"守卫/":"");
		return this;
	}
	//输入player信息
	public void inputPlayer(Player player){
		playerMap.put(player.id+"", player);
		if(player.role instanceof Witch){
			witch=player;
		}

		if(player.role instanceof Forsee){
			forsee=player;
		}

		if(player.role instanceof Guard){
			guard=player;
		}
		if(player.role instanceof Hunter){
			hunter=player;
		}
	}
	//是否有player信息
	public boolean hasPlayer(Player player){
		if(playerMap.get(player.id+"")!=null){
			return true;
		}else{
			return false;
		}
	}
	//验证player信息
	public boolean validatePlayer(Player player){
		boolean isSuccess=true;
		if(playerMap.get(player.id+"")!=null){
			if(!playerMap.get(player.id+"").password.equals(player.password)){
				isSuccess=false;
			}else{
				//修改黑夜中弹出后，再进入白天后无法判断为醒来的bug
				playerMap.get(player.id+"").state=day;
			}
		}
		
		return isSuccess;
	}
	//进入黑夜
	public synchronized boolean startNight(String number){
		if(minState()+1<=day+0.5)return false;
		if(!isnight){
			night=new Night();
			isnight=true;
		}
		playerMap.get(number).state=day+0.5;
		return true;
	}
	private double minState(){
		Player player;
		double min=Double.MAX_VALUE;
		for(int i=0;i<number;i++){
			player=playerMap.get((i+1)+"");
			if(!player.isOut&&player.state<min){
				min=player.state;
			}
		}
		return min;
	}
	//进入白天
	public synchronized boolean day(String number){
		//判断游戏是否结束
		
		//
		if(
			(night.wolfready)&&
			(night.forseeready||forsee==null||forsee.isOut)&&
			(night.guardready||guard==null||guard.isOut)&&
			(night.witchready||witch==null||witch.isOut)&&
			isnight){
				isnight=false;
				judge();
				day++;
		}
		if(!isnight){
			playerMap.get(number).state=day;
		}
		return !isnight;
	}
	private void judge(){
		Player killed;
		try{
			killed = playerMap.get(night.killed.get("number").getAsString());
		}catch(Exception e){
			killed=null;
		}
		boolean isGuard=false;
		try{
			isGuard=night.killed.get("number").getAsString().equals(night.guarded.get("number").getAsString());
		}catch(Exception e){
			System.out.println("没有守卫！");
		}
		//狼人
		if(night.killed.get("number")!=null){
			playerMap.get(night.killed.get("number").getAsString()).iskilled=true;
		}
		//女巫救人
		if(night.isHeal){
			((Witch)(witch.role)).heal(killed, isGuard);
		}else if(isGuard){
		//守卫守对人
			if(night.killed.get("number")!=null){
				playerMap.get(night.killed.get("number").getAsString()).iskilled=false;
			}
		}
		isFinished();
		//毒药
		if(night.poisoned!=null){
			((Witch)witch.role).poison(playerMap.get(night.poisoned.get("number").getAsString()));
		}
		isFinished();
		//上帝的话
		godword="今晚";
		Player temp;
		boolean hasDead=false;
		for(int i=0;i<number;i++){
			temp = playerMap.get((i+1)+"");
			if(!temp.isOut&&temp.iskilled){
				godword+="【"+temp.id+"号"+temp.name+"】";
				hasDead=true;
				temp.isOut=true;
			}
		}
		
		if(hasDead){
			try{
				godword+="死了。猎人"+(((Hunter)hunter.role).canShoot?"":"不")+"可以发动技能。";
			}catch(Exception e){
				godword+="死了。";
			}
		}else{
			godword+="平安夜。";
		}
		if(finish!=null){
			godword+=finish;
		}
	}
	//获取playerList
	public synchronized JsonArray getPlayerList(){
		JsonArray array=new JsonArray();
		JsonObject object;
		Player player;
		for (int i=0;i<number;i++) {
			object=new JsonObject();
			player = playerMap.get((i+1)+"");
			if(player==null)continue;
			object.addProperty("number", player.id);
			object.addProperty("name", player.name);
			object.addProperty("out", player.isOut);
			object.addProperty("good", player.role.isGood);
			array.add(object);
		}
		return array;
	}
	//狼人杀人
	public synchronized boolean kill(JsonObject person) {
		if(!night.hasKilled){
			night.killed=person;
			night.hasKilled=true;
			return true;
		}else {
			return false;
		}
	}
	public void reset() {
		witch=null;
		guard=null;
		forsee=null;
		hunter=null;
		godword="";
		night=null;
		day=1;
		playerMap.clear();
		isnight=false;
		finish=null;
	}
	public synchronized boolean gameReady() {
		//玩家数量
		if(playerMap.size()!=number)return false;
		//狼人数量
		Player player;
		int wn=0;
		int cn=0;
		boolean hasForsee=false;
		boolean hasWitch=false;
		boolean hasHunter=false;
		boolean hasIdiot=false;
		boolean hasGuard=false;
		for(int i=1;i<number+1;i++){
			player=playerMap.get(i+"");
			if(player.role instanceof Citizen)cn++;
			if(player.role instanceof Wolf)wn++;
			if(player.role instanceof Forsee){
				if(hasForsee){
					return false;
				}
				hasForsee=true;
			}
			if(player.role instanceof Witch){
				if(hasWitch){
					return false;
				}
				hasWitch=true;
			}
			if(player.role instanceof Hunter){
				if(hasHunter){
					return false;
				}
				hasHunter=true;
			}
			if(player.role instanceof Idiot){
				if(hasIdiot){
					return false;
				}
				hasIdiot=true;
			}
			if(player.role instanceof Guard){
				if(hasGuard){
					return false;
				}
				hasGuard=true;
			}
		}
		if(wn!=wolfnumber)return false;
		if(cn!=citizennumber)return false;
		
		return true;
	}
	public synchronized boolean isFinished(){
		if(finish!=null)return true;
		boolean haswolf=false;
		boolean hasgod=false;
		boolean hascitizen=false;
		Player player;
		for(int i=1;i<number+1;i++){
			player=playerMap.get(i+"");
			if(!player.isOut&&!player.iskilled){
				if(!player.role.isGood){
					haswolf=true;
				}else if(player.role.isGod){
					hasgod=true;
				}else if(!player.role.isGod){
					hascitizen=true;
				}
			}
		}
		if(!haswolf){
			finish= "游戏结束，好人胜利！";
			return true;
		}else if(!hasgod||!hascitizen){
			finish= "游戏结束，狼人胜利！";
			return true;
		}

		return false;
	}
	public JsonObject gettips() {
		JsonObject result=new JsonObject();
		result.addProperty("wolfready", night.wolfready);
		if(witch!=null){
			result.addProperty("witchready", (witch.isOut?true:night.witchready));
			result.addProperty("witchdead", witch.isOut);
		}
		if(forsee!=null){
			result.addProperty("forseeready",(forsee.isOut?true:night.forseeready));
			result.addProperty("forseedead", forsee.isOut);
		}
		if(guard!=null){
			result.addProperty("guardready", (guard.isOut?true:night.guardready));
			result.addProperty("guarddead", guard.isOut);
		}
		return result;
	}
}
